package cn.itcast.service.system.impl;

import cn.itcast.dao.system.RoleDao;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAll(String companyId) {
        return roleDao.findAll(companyId);
    }

    @Override
    public Role findById(String id) {
        return roleDao.findById(id);
    }

    @Override
    public void save(Role role) {
        roleDao.save(role);
    }

    @Override
    public void updateById(Role role) {
        roleDao.updateById(role);
    }

    @Override
    public void deleteById(String id) {
        roleDao.deleteById(id);
    }

    @Override
    public PageInfo<Role> findByPage(Integer page, Integer pageSize, String companyId) {
        PageHelper.startPage(page, pageSize);
        List<Role> list = roleDao.findAll(companyId);
        return new PageInfo<>(list, 5);
    }

    @Override
    public String findRoleByUserid(String id) {
        List<String> roles = roleDao.findRoleByUserid(id);
        StringBuilder userRoleStr = new StringBuilder();
        for (String role : roles) {
            userRoleStr.append(role);
        }
        return userRoleStr.toString();
    }
}
